<?php


namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Validator;
use App\Model\Users;
use App\Model\Company;
use App\Model\Dangerrecords;

use PhpOffice\PhpWord\TemplateProcessor;
use App\Services\Export\WordChoose;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use Storage;



class DangerrecordsController extends Controller
{


    /**
     * 文件上传-图片/视频
     * 公共接口已存在
     * /api/upload/uploadimg
     * /api/upload/uploadmp4
     */


     /**
      * 随手拍记录列表
      *page  limit 
     */

     public function list(Request $request){
        // 查询当前登录人提交的全部随手拍图片记录
        $list = Dangerrecords::where('user_id',Auth::user()->id)->where('is_delete',1)->paginate($request->get('limit',10));
        foreach($list->items() as &$v){
         $v['images'] = explode(',',$v['images']);
        }
        return response()->json(array('data'=>$list->items(),'message'=>'查询成功','code'=>200),200);
     }

     /**
      * 获取单条数据
      * 图片是多图上传，视频是单个
     */
    public function data(Request $request){
      if(empty($request['id'])){
         return response()->json(array('data'=>array(),'message'=>'ID不能为空','code'=>400),400);
      }
      $data = Dangerrecords::findorfail($request['id']);
      return response()->json(array('data'=>$data,'message'=>'查询成功','code'=>200),200);
    }

    /**
      * 添加/修改随手拍记录
      * 图片是多图上传，用，分隔，视频是单个
     */
    public function edit(Request $request){

      $rules = [
         'content' => 'required',
         'lng' => 'required',
         'lat' => 'required',
         'address' => 'required',
      ];
      $messages = [
            'content.required' => '描述未填写',
            'lng.required' => '经度不能为空',
            'lat.required' => '维度不能为空',
            'address.required' => '地址信息不能为空',
      ];
      $validator = Validator::make($request->all(),$rules,$messages);
      if ($validator->fails()) {
            return response()->json(['code'=>0,'msg' => $validator->errors()->first()], 200);
      }


      if(empty($request['id'])){
			// 新增
			$model = new Dangerrecords();
		}else{
			// 编辑
			$model = Dangerrecords::findorfail($request['id']);
			if(!$model){
				return response()->json(array('data'=>array(),'message'=>'数据不存在','code'=>400),400);
			}
		}
      $type=1;
      if(!empty($request['images']) && !empty($request['video'])){
         $type=3;
      }else if(empty($request['images']) && !empty($request['video'])){
         $type=2;
      }

      $company_id = Company::where('userId',Auth::user()->id)->pluck('id');
		$model->images = $request['images'];
		$model->video = $request['video'];
		$model->videoname = $request['videoname'];
      
		$model->content = $request['content'];
		$model->type = $type;
		$model->lng = $request['lng'];
		$model->lat = $request['lat'];
		$model->address = $request['address'];
		$model->user_id = Auth::user()->id;
		$model->company_id = $company_id;
		$model->save();
      return response()->json(array('data'=>array(),'message'=>'操作成功','code'=>200),200);
    }

     /**
      * 删除随手拍记录
     */

     public function delete(Request $request){
      if(empty($request['id'])){
         return response()->json(array('data'=>array(),'message'=>'ID不能为空','code'=>400),400);
      }
      $model = Dangerrecords::findorfail($request['id']);
      $model->is_delete = 2;
      $model->save();
      return response()->json(array('data'=>array(),'message'=>'删除成功','code'=>200),200);
     }

}